<?php

/**
 * BlogLink filter form.
 *
 * @package    HugoHamon
 * @subpackage filter
 * @author     Hugo Hamon <hugo.hamon@sensio.com>
 * @version    SVN: $Id: sfDoctrineFormFilterTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class BlogLinkFormFilter extends BaseBlogLinkFormFilter
{
  protected $languages;

  public function configure()
  {
    $this->widgetSchema['lang'] = new sfWidgetFormI18nChoiceLanguage(array(
      'languages' => $this->getLanguages(),
      'add_empty' => true
    ));

    $this->useFields(array('name', 'lang', 'is_published'));
  }
  
  protected function getLanguages()
  {
    if (count($this->languages))
    {
      return $this->languages;
    }

    $languages = Doctrine_Core::getTable('BlogLink')->
      createQuery('l')->
      distinct(true)->
      select('l.lang')->
      groupBy('l.lang')->
      orderBy('l.lang asc')->
      execute(array(), Doctrine_Core::HYDRATE_ARRAY)
    ;

    foreach ($languages as $language)
    {
      $this->languages[] = $language['lang'];
    }

    return $this->languages;
  }
  
  protected function addLangColumnQuery(Doctrine_Query $q, $field, $value)
  {
    $rootAlias = $q->getRootAlias();

    $q->addWhere($rootAlias.'.lang = ?', $value);

    return $q;
  }
}
